package com.wf.netty.order;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.Charset;

/**
 * 入站
 *
 * @author wf
 * @since 2022/5/13
 */
public class EchoInboundHandler extends ChannelInboundHandlerAdapter {
    private String name;

    private boolean goFlush;

    public EchoInboundHandler() {
    }
    public EchoInboundHandler(String name, boolean goFlush) {
        this.name = name;
        this.goFlush = goFlush;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        System.out.println("read: " + this.name);
        if (goFlush) {
            System.out.println("flush: " + this.name);
            ctx.channel().writeAndFlush(in.toString(Charset.defaultCharset()));
        } else {
            super.channelRead(ctx, msg);
        }
    }
}
